#!/bin/sh
# PCP QA Test No. 1784
# Exercise interactive pmlogconf saving changes.
#
# Copyright (c) 2021 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

# need these metrics for pmlogctl predicates to be deterministic
#
_need_metric mmv.control.files
_need_metric pmproxy.control.files

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e "s,$tmp.config,TMP.config,g" \
	-e "s,$tmp.pmlogconf,GROUPDIR,g" \
	-e "s/[0-9][0-9]*-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:.*/TIMESTAMP/g" \
	 -e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]/TIMESTAMP/' \
    # end
}

# real QA test starts here

echo "Create a well-known set of pmlogconf templates"
mkdir -p $tmp.pmlogconf/qa
cat >$tmp.pmlogconf/qa/mmv <<EOF
#pmlogconf-setup 2.0
ident	Instrumented applications summary information
probe	mmv.control.files exists ? include : exclude
	mmv
EOF
cat >$tmp.pmlogconf/qa/hinv <<EOF
#pmlogconf-setup 2.0
ident	Instrumented applications summary information
probe	hinv.ncpu >= 1 ? include : exclude
	hinv
EOF
cat >$tmp.pmlogconf/qa/pmproxy <<EOF
#pmlogconf-setup 2.0
ident	Instrumented applications summary information
probe	pmproxy.control.files exists ? include : exclude
	pmproxy
EOF
head $tmp.pmlogconf/qa/* >> $seq_full
pmlogconf -d $tmp.pmlogconf $tmp.config 2>&1 | _filter
echo "== Initial pmlogger config created" | tee -a $seq_full
cat $tmp.config | tee -a $seq_full | _filter

echo "== Modify deltas and ensure changed values persist"
cat >$tmp.expect <<EOF
yes
1 minute
yes
5 seconds
no
yes
EOF
cat $tmp.expect \
| pmlogconf -d $tmp.pmlogconf $tmp.config \
| tee -a $seq_full \
| _filter
echo
echo

echo "== Completed pmlogger configuration"
cat $tmp.config | _filter

# success, all done
exit
